package com.topway.dsmflex.thread.job;

import com.topway.dsmflex.Log;
import com.topway.dsmflex.model.base.DataMonitorBean;
import com.topway.dsmflex.model.base.DataPropertyBean;


/**
 * 单个表同步线程
 * 
 * @author liusy 2013-05-09
 *
 */
public abstract class AbstractDataThread extends Thread{

	private DataPropertyBean _dt_123 = null;
	protected SynchroManager sm = null;
	
	private boolean flag = true;
	protected int result = 0;
	private long nterval =0;
	private int count = 0;
	private boolean isRun = true;
	
	public AbstractDataThread(SynchroManager sm,DataPropertyBean dt)
	{
		this._dt_123 = dt;
		this.sm=sm;
		this.flag = true;
	}



	@Override
	public void run() {
		String _nterval= String.valueOf((1000*60*_dt_123.getSynchroNterval()));
		nterval=_nterval.indexOf(".")==-1?Long.parseLong(_nterval):Long.parseLong(_nterval.substring(0, _nterval.indexOf(".")));
		Log.clearLog();
		while(isRun){
			if(flag)
			{
				
				count=0;
				_dt_123.propertyChange(DataMonitorBean.STATE,"2","1");
				
				this.result = SynchroData();
				
				
				try {
					//正在休息
					_dt_123.propertyChange(DataMonitorBean.STATE,"1","2");
					this.sleep(nterval);
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.gc();
			}
			else
			{
				//准备销毁
				_dt_123.propertyChange(DataMonitorBean.STATE,"1","3");
				count++;
				try {
					this.sleep(1000);
				} catch (InterruptedException e) {
					Log.info(e.getMessage());
				}
				if(count>3)//3秒自动销毁
				{
					//已经停止
					_dt_123.propertyChange(DataMonitorBean.STATE,"1","4");
					Log.warning("任务："+_dt_123.getName()+"  已经停止!");
					isRun=false;
					this.interrupt();
				}
			}
		}
	}
	
	
	public abstract int SynchroData();

	
	public void suspendSynchro()
	{
		
		this.flag = false;
		this.nterval=0;
//		try {
//			//已经停止
//			isRun=false;
//				Log.warning("任务："+_dt_123.getName()+"  已经停止!");
//				this.interrupt();
//		} catch (Exception e) {
//			Log.warning("任务："+_dt_123.getName()+"  停止!  异常!");
//		}finally{
//			_dt_123.propertyChange(DataMonitorBean.STATE,"1","4");
//		}
	
	}
	
	public void  resumeSynchro()
	{
		this.flag = true;
	}
	
	
}
